package sample;
import javafx.scene.control.TextArea;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;

import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.*;
import javafx.event.ActionEvent;

import java.util.LinkedList;

public  class Controller  implements Cloneable  {
    @FXML
    private TextField txtname,txtpriority,txttime;
    @FXML
    private Button onadd,onRR,onPRI,onSPN,onSRT;
    @FXML
    public  TextArea txtprocess;
    private   LinkedList<PCB> pcblist = new LinkedList<PCB>();
    private int space=10;//空闲PCB数量
    public void onaddaction(ActionEvent event) {
        if (txtname.getText().isEmpty()||
                txtpriority.getText().isEmpty()||
                txttime.getText().isEmpty()) {//不完整
            txtname.setText("");
            txtpriority.setText("");
            txttime.setText("");
            JOptionPane.showMessageDialog(null,"还有参数未填写");
        }else if (Integer.parseInt(txttime.getText().trim())<0||
                Integer.parseInt(txtpriority.getText().trim())<0 ||
                space<=0){
                if (Integer.parseInt(txttime.getText().trim())<0)
                    JOptionPane.showMessageDialog(null,"剩余时间不能小于0");
                if (Integer.parseInt(txtpriority.getText().trim())<0)
                    JOptionPane.showMessageDialog(null,"优先级不能小于0");
                if (space<=0)
                    JOptionPane.showMessageDialog(null,"无空闲PCB");
            txtname.setText("");
            txtpriority.setText("");
            txttime.setText("");
        }
        else {
            for (int i=0;i<pcblist.size();i++){
                if (pcblist.get(i).getName()==Integer.parseInt(txtname.getText())){
                    txtname.setText("");
                    txtpriority.setText("");
                    txttime.setText("");
                    JOptionPane.showMessageDialog(null,"该进程标识符已存在");
                    return;
                }
            }
            PCB pcb1 = new PCB(Integer.parseInt(txtname.getText()), 0,
                    Integer.parseInt(txtpriority.getText()),
                    Integer.parseInt(txttime.getText()));
            pcblist.add(pcb1);
            space--;
            txtname.setText("");
            txtpriority.setText("");
            txttime.setText("");
            JOptionPane.showMessageDialog(null,"添加成功");
        }
    }


    public void onRRaction (ActionEvent event){
        LinkedList<PCB> pcblist_copy = new LinkedList<PCB>();
        Iterator<PCB> it = pcblist.iterator();
        while (it.hasNext()) {
            pcblist_copy.add((it.next()).clone());
        }//深度克隆
        txtprocess.setText("");//将上一次结果清空
        RR rr = new RR(pcblist_copy,txtprocess);
    }
    public void onPRIaction(ActionEvent event){
        LinkedList<PCB> pcblist_copy = new LinkedList<PCB>();
        Iterator<PCB> it = pcblist.iterator();
        while (it.hasNext()) {
            pcblist_copy.add((it.next()).clone());
        }//深度克隆
        txtprocess.setText("");//将上一次结果清空
        PRI pri = new PRI(pcblist_copy,txtprocess);
    }
    public void onSPNaction(ActionEvent event){
        LinkedList<PCB> pcblist_copy = new LinkedList<PCB>();
        Iterator<PCB> it = pcblist.iterator();
        while (it.hasNext()) {
            pcblist_copy.add((it.next()).clone());
        }//深度克隆
        txtprocess.setText("");//将上一次结果清空
        new Thread(new NewSPN(new ArrayList<>(pcblist_copy),txtprocess)).run();
    }
    public void onSRTaction(ActionEvent event){
        LinkedList<PCB> pcblist_copy = new LinkedList<PCB>();
        Iterator<PCB> it = pcblist.iterator();
        while (it.hasNext()) {
            pcblist_copy.add((it.next()).clone());
        }//深度克隆
        txtprocess.setText("");//将上一次结果清空
        SRT srt = new SRT(pcblist_copy,txtprocess);
    }
}
